package com.example.restatr.Atcivity.GovServiceHotline;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MyAppealAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private ListView lv;
    private LinearLayout Null;
    private ImageView nullIcon;
    private TextView nullText;
    private LinearLayout loading;
    private ProgressBar loadingBar;
    private TextView loadingText;
    private List<GovServiceAppealBean.RowsBean> rowsBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_appeal_at);
        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        lv = findViewById(R.id.lv);
        Null = findViewById(R.id.Null);
        nullIcon = findViewById(R.id.null_icon);
        nullText = findViewById(R.id.null_text);
        nullText.setText("暂无诉求...");
        loading = findViewById(R.id.loading);
        loadingBar = findViewById(R.id.loading_bar);
        loadingText = findViewById(R.id.loading_text);
        Tools.SetBarTitle(toolbar, barTitle, "我的诉求", search, this,false);
    }

    private void GetRows() {
        rowsBeanList.clear();
        new Thread(() -> {
            String id = Tools.Get("/prod-api/api/gov-service-hotline/appeal/my-list", MainActivity.sp.getString("token", ""));
            GovServiceAppealBean govServiceAppealBean = new Gson().fromJson(id, GovServiceAppealBean.class);
            rowsBeanList.addAll(govServiceAppealBean.getRows());
            runOnUiThread(() -> {
                if (rowsBeanList.size() > 0) {
                    lv.setAdapter(new adapter());
                    loading.setVisibility(View.GONE);
                } else {
                    Null.setVisibility(View.VISIBLE);
                }
            });
        }).start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        GetRows();
    }

    public void onClick(View view) {
        startActivity(new Intent(this, CreateAppealAt.class));
    }

    class adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return rowsBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return rowsBeanList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.service_info_item, null);
            TextView title = convertView.findViewById(R.id.title);
            TextView undertaker = convertView.findViewById(R.id.undertaker);
            TextView name = convertView.findViewById(R.id.name);
            TextView date = convertView.findViewById(R.id.date);
            TextView content = convertView.findViewById(R.id.content);
            ImageView img = convertView.findViewById(R.id.img);
            CardView reply = convertView.findViewById(R.id.reply);
            TextView replyContent = convertView.findViewById(R.id.replyContent);

            title.setText(rowsBeanList.get(position).getTitle());
            undertaker.setText("承办单位：" + rowsBeanList.get(position).getUndertaker());
            name.setText("用户ID：" + rowsBeanList.get(position).getUserId());
            date.setText(rowsBeanList.get(position).getCreateTime());
            content.setText(rowsBeanList.get(position).getContent());
            Glide.with(MyAppealAt.this)
                    .load(Tools.ip + rowsBeanList.get(position).getImgUrl())
                    .transform(new RoundedCorners(15))
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<Drawable> target, boolean b) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable drawable, Object o, Target<Drawable> target, DataSource dataSource, boolean b) {
                            img.setImageDrawable(drawable);
                            img.setVisibility(View.VISIBLE);
                            return true;
                        }
                    })
                    .into(img);
            if (rowsBeanList.get(position).getState().equals("1")) {
                reply.setVisibility(View.VISIBLE);
                replyContent.setText(rowsBeanList.get(position).getDetailResult());
            }
            return convertView;
        }
    }

}